# Krause/Krause/Bränfors
# Women’s Participation in Peace Negotiations and the Durability and Quality of Peace

# Author: Werner Krause
# E-Mail: werner.krause@wzb.eu
# Date: 02/2018

# > sessionInfo()
# R version 3.4.3 (2017-11-30)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS High Sierra 10.13.2
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] ggplot2_2.2.1 scales_0.5.0  bindrcpp_0.2  dplyr_0.7.4   magrittr_1.5  sp_1.2-5     
# 
# loaded via a namespace (and not attached):
# [1] Rcpp_0.12.14     bindr_0.1        hms_0.4.0        tidyselect_0.2.3 munsell_0.4.3   
# [6] colorspace_1.3-2 lattice_0.20-35  R6_2.2.2         rlang_0.1.6      stringr_1.2.0   
# [11] plyr_1.8.4       tools_3.4.3      grid_3.4.3       gtable_0.2.0     yaml_2.1.16     
# [16] lazyeval_0.2.1   readxl_1.0.0     assertthat_0.2.0 digest_0.6.13    tibble_1.3.3    
# [21] countrycode_0.19 readr_1.1.1      purrr_0.2.4      tidyr_0.7.2      ggrepel_0.7.0   
# [26] glue_1.2.0       haven_1.1.0      labeling_0.3     stringi_1.1.5    compiler_3.4.3  
# [31] cellranger_1.1.0 forcats_0.2.0    pkgconfig_2.0.1 

library( dplyr )

# Figure 2

PAM <- readxl::read_excel( 'data/PAM/PAM_ID%20V.1.5%20Updated%2029JULY2015-3.xlsx' ) %>%
  select( pam_caseid , country , country_accorddate , year_count , `Fem. Signatories` , agg_implem_score ) %>%
  mutate( iso2c = countrycode::countrycode( country , 'country.name' , 'iso2c' , T )
          , year = stringr::str_extract( country_accorddate , '[:digit:][:digit:][:digit:][:digit:]' )
          , country_year = paste( iso2c , year ))



library( magrittr )
library( dplyr )

PAM %<>% filter( year_count == 10 )  
med.fs <- median( PAM$agg_implem_score[ PAM$`Fem. Signatories` == 1 ] , na.rm = T )  
med.no.fs <- median( PAM$agg_implem_score[ PAM$`Fem. Signatories` == 0 ] , na.rm = T )
PAM$country_year <- factor( PAM$country_year, levels=unique( PAM$country_year[order( PAM$agg_implem_score )]), ordered=TRUE)

library( ggplot2 )

PAM %>% 
  ggplot( aes( x = country_year , y = agg_implem_score )) +
  geom_hline( yintercept = med.fs , linetype = 2 , color = 'grey50' ) +
  geom_hline( yintercept = med.no.fs , linetype = 2 , color = 'grey50' ) +
  geom_point( data = PAM , aes( color = factor( `Fem. Signatories` )) , size = 3 ) +
  scale_color_manual( name = '' 
                      , values = c( 'red' , 'black' ) 
                      , labels = c( 'without Women Signatories' , 'with Women Signatories' )) +
  scale_x_discrete( name = '' ) +
  scale_y_continuous( name = 'Implementation Rate' , limits = c( 0 , 100 ) ) +
  coord_flip( ) +
  theme( axis.text.y = element_blank( )
         , axis.ticks.y = element_blank( )
         , axis.line = element_line( color = 'black' ))

detach( package:ggplot2 )


# Figure 3

PAM <- readxl::read_excel( 'data/PAM/PAM_ID%20V.1.5%20Updated%2029JULY2015-3.xlsx' )
PAM.prov <- PAM %>% 
  filter( year_count == 1 ) %>%
  select( country , `Fem. Signatories` , contains( "prov" )) %>%
  tidyr::gather( variable , value , women_prov : amnest_prov ) %>%
  group_by( `Fem. Signatories` , variable ) %>%
  mutate( total_number = max( seq_along( variable ))) %>% 
  mutate( sum_value = sum( value )) %>% 
  ungroup( ) %>%
  mutate( share = sum_value / total_number ) %>%
  select( variable , `Fem. Signatories` , share ) %>%
  unique( ) %>%
  mutate( type = 'Share of Accords with Provisions' ) %>%
  as.data.frame( )

PAM.impl <- PAM %>% 
  filter( year_count == 10 ) %>%
  select( country , `Fem. Signatories` , contains( "implem" ) , contains( 'iplem' ) , contains( 'impem' )) %>%
  select( -total_implem_points , -agg_implem_score ) %>% 
  tidyr::gather( variable , value , amnest_implem : civadm_impem ) %>%
  group_by( `Fem. Signatories` , variable ) %>%
  mutate( share = mean( value , na.rm = T )) %>%
  ungroup( ) %>%
  select( variable , `Fem. Signatories` , share ) %>%
  unique( ) %>%
  mutate( type = 'Implementation Rate' ) %>%
  as.data.frame( )


PAM.prov %<>% 
  rbind( PAM.impl ) %>%
  mutate( variable = stringr::str_replace( variable , '_.*' , '' )) %>% 
  mutate( order = ifelse( `Fem. Signatories` == 1 & type == 'Share of Accords with Provisions' , share , NA )) %>% 
  group_by( variable ) %>%
  mutate( order = max( order , na.rm = T )) %>%
  ungroup( ) %>%
  mutate( variable = ifelse( variable == 'dispute' , 'disput' , variable ))

PAM.prov %>% filter( `Fem. Signatories`  == 0 & type == 'Share of Accords with Provisions' ) -> PAM1
PAM.prov %>% filter( `Fem. Signatories`  == 1 & type == 'Share of Accords with Provisions' ) -> PAM2
PAM.prov %>% filter( `Fem. Signatories`  == 0 & type != 'Share of Accords with Provisions' ) -> PAM3
PAM.prov %>% filter( `Fem. Signatories`  == 1 & type != 'Share of Accords with Provisions' ) -> PAM4
PAM1$variable <- factor( PAM1$variable , levels=PAM1[order( PAM1$order , decreasing = F ),]$variable )
PAM2$variable <- factor( PAM2$variable , levels=PAM2[order( PAM2$order , decreasing = F ),]$variable )
PAM3$variable <- factor( PAM3$variable , levels=PAM3[order( PAM3$order , decreasing = F ),]$variable )
PAM4$variable <- factor( PAM4$variable , levels=PAM4[order( PAM4$order , decreasing = F ),]$variable )

PAM.prov <- rbind( PAM1 , PAM2 , PAM3 , PAM4 )

PAM.prov$type <- as.factor( PAM.prov$type )
PAM.prov$type <- ordered( PAM.prov$type 
                          , levels = c( 'Share of Accords with Provisions' , 'Implementation Rate' ))

library( scales )
library( ggplot2 )


PAM.prov %>%
  ggplot( aes(  y = share , fill = as.factor( `Fem. Signatories` ) , x = as.factor( variable ))) +
  geom_bar( position = 'dodge' , stat = 'identity' , width = .75 ) +
  scale_fill_manual( name = '' 
                     , values = c( 'red' , 'black' ) 
                     , labels = c( 'Peace Agreements without Women Signatories' 
                                   , 'Peace Agreements with Women Signatories' )) +
  scale_x_discrete( name = '' ) +
  facet_wrap( ~type , scales = 'free_x' ) +
  scale_y_continuous( name = '' ) +
  coord_flip( )  +
  guides( fill = guide_legend( nrow = 2 )) +
  theme( legend.title = element_text( '' )
         , panel.grid.major.y = element_blank( ))

detach( package:ggplot2 )
detach( package:scales )
